package com.baizhi.realm;

import com.baizhi.dao.AdminDAO;
import com.baizhi.entity.Admin;
import org.apache.shiro.authc.*;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.springframework.beans.factory.annotation.Autowired;

public class AuthenRealm extends AuthenticatingRealm {
    @Autowired
    private AdminDAO adminDAO;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
            throws AuthenticationException {
//        从令牌中获取用户名
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        String username = usernamePasswordToken.getUsername();
//        根据名字查询查询数据库
        Admin admin = adminDAO.selectOne(username);
//        如果对象不为null  封装info
        if(admin!=null){
//            参数一  数据库查到的账号
//            参数二  数据库查到的密码
//            参数三  当前对象的名字
            return new SimpleAuthenticationInfo(admin.getUsername(),
                    admin.getPassword(), this.getName());
        }
        return null;
    }
}
